/**
 * 项目管理
 */
var project={
		ctx : "",
		/**查询项目列表*/
		queryProjestList : function(){
//			location.reload() ;
			var projectName = $("#projectName").val();
			var customerCompanyId = $("#customerCompanyId").val();
			var staffId = $("#staffId1").val();
			var partnerCompanyName = $("#partnerCompanyName").val();
			var projectStatus = $("#projectStatus").val();
			
			var url ="projectPage?projectStatus="+projectStatus;
			if(!projectName == ""){
				url += "&projectName="+projectName;
			}
			if(!customerCompanyId == ""){
				url += "&customerCompanyId="+customerCompanyId;
			}
			if(!staffId== ""){
				url += "&staffId="+staffId;
			}
			if(!partnerCompanyName== ""){
				url += "&partnerCompanyName="+partnerCompanyName;
			}
			location.href = url;
		},
		/**确定立项*/
		projectApproval : function(){
			var projectId = $("#projectId").val();
			var offer = $("#offer").val();
			if(!project.isNum(offer)){
				return false;
			}
			project.putBeforeFormJson();
			if(""!=projectId){
				var url = project.ctx + "/project/projectApproval";
				$('#beforProjectForm').attr("action", url);
				$('#beforProjectForm')[0].submit();
			}else{
				$('#errorMsg').html("没有找到相应的项目数据！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
			}
		},
		/**废除项目*/
		revokeProject : function(){
			var projectId = $("#projectId").val();
			if(""!=projectId){
				var url ="revokeProject?projectId="+projectId;
				location.href = url;
			}else{
				$('#errorMsg').html("没有找到相应的项目数据！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
			}
		},
		/**废除项目管理员*/
		revokeProjectAdmin : function(){
			var projectId = $("#projectId").val();

			if(""!=projectId){
				var url ="revokeProjectAdmin?projectId="+projectId;
				location.href = url;
			}else{
				$('#errorMsg').html("没有找到相应的项目数据！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
			}
		},
		/**项目完成*/
		projectFinish : function(){
			//添加必填条件校验
//			$("#flag").val("4");
			project.putImplFormJson();
			var projectId = $("#projectId").val();
			
			if(""!=projectId){
				var url = project.ctx + "/project/projectFinish";
				$('#implProjectForm').attr("action", url);
				$('#implProjectForm')[0].submit();
//				location.href = url;
			}else{
				$('#errorMsg').html("没有找到相应的项目数据！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
			}
		},
		/**进入运维管理页*/
		toRepairPage : function(){
			var projectId = $("#projectId").val();
			if(""!=projectId){
				var url =project.ctx + "/projectRepaird/toPage?projectId="+projectId;
				location.href = url;
//				window.open(url);
			}else{
				$('#errorMsg').html("没有找到相应的项目运维数据！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
			}
		},
		/**添加合作厂商*/
		/*addPartner : function(){
			var partnerCompanyId = $("#partnerCompanyId").val();
			if(""==partnerCompanyId){
//				alert("请选择要添加的合作厂商");
				$('#errorMsg').html("请选择要添加的合作厂商！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
				return;
			}
			var partnerCompanyName = $("#partnerCompanyId").find("option:selected").text();
			var str = "<tr x-insert><td hidden><input type='hidden' name='partnerDetailId' value='' />"
					+"<input type='hidden' name='partnerCompanyId' value='"+partnerCompanyId+"' /></td>"
					+"<td ><input type='text' name='partnerCompanyName' class='am-input-sm' value='"+partnerCompanyName+"' disabled></td></tr>"; 
//			var tpl = Handlebars.compile($('#tpl-table1').html());
//		    $('#partnerTable tbody').append(tpl());
		    $('#partnerTable tbody').append(str);
		},*/
		/**添加供应商*/
		addPartnerCompany : function(){
			var tpl = Handlebars.compile($('#tpl-table3').html());
		    $('#partnerCompanyTable tbody').append(tpl());
		},
		/**添加售前日志*/
		addLog : function(){
			var tpl = Handlebars.compile($('#tpl-table2').html());
		    $('#logTable tbody').append(tpl());
		},
		/**添加合同开票明细*/
		addBillingContractDetail : function(){
			var tpl = Handlebars.compile($('#tpl-tableBilling').html());
		    $('#contractBillingDetailTable tbody').append(tpl());
		    project.initRate();
		},
		/**添加合同收款明细*/
		addPaymentContractDetail : function(){
			var tpl = Handlebars.compile($('#tpl-tablePayment').html());
		    $('#contractPaymentDetailTable tbody').append(tpl());
		},
		/**提交售前表单*/
		submitBeforeForm: function(){
			var offer = $("#offer").val();
			if(!project.isNum(offer)){
				return false;
			}
			project.putBeforeFormJson();
//			$('#beforProjectForm').serialize();
			$('#beforProjectForm')[0].submit();
		},
		/**提交执行表单*/
		submitImplForm: function(){
			var bilcount = 0;
			$("input[name=billingRate]").each(function(i, v){
//				bilcount = parseInt(bilcount) + parseInt(this.value);
				bilcount = bilcount*1 + this.value*1;
			});
			if(bilcount>100){
				$('#errorMsg').html("开票比例总和不能超过100%");
				var $modal1 = $('#my-alert');
				$modal1.modal();
				return false;
			}
			var paycount = 0;
			$("input[name=paymentRate]").each(function(i, v){
				paycount = parseInt(paycount) + parseInt(this.value);
//				paycount = paycount*1 + this.value*1;
			});
			if(paycount>100){
				$('#errorMsg').html("收款比例总和不能超过100%");
				var $modal1 = $('#my-alert');
				$modal1.modal();
				return false;
			}
			project.putImplFormJson();
//			$('#implProjectForm').serialize();
			$('#implProjectForm')[0].submit();
		},
		/**添加合作商和售前日志信息*/
		putBeforeFormJson: function(){
			var logUpdateList = [];
			var logInsertList = [];
			var partnerInsertList = [];
			$('#logTable tbody tr[x-update]').each(function(i, v){
				var oi = {};
				$(this).find('input').each(function(i, v){
					if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
				});
				$(this).find('textarea').each(function(i, v){
					oi[this.name] = this.value;
				});
				logUpdateList[i] = oi;
		    });
			$('#logTable tbody tr[x-insert]').each(function(i, v){
		        var oi = {};
		        $(this).find('input').each(function(i, v){
		        	if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
		        });
				$(this).find('textarea').each(function(i, v){
					oi[this.name] = this.value;
				});
		        logInsertList[i] = oi;
		    });
			$('#partnerCompanyTable tbody tr').each(function(i, v){
				var oi = {};
				$(this).find('select').each(function(i, v){
					oi[this.name] = $(this).val();
				});
				partnerInsertList[i] = oi;
		    });
			
			$('#beforProjectForm input[name=logUpdateListJson]').val(JSON.stringify(logUpdateList));
			$('#beforProjectForm input[name=logInsertListJson]').val(JSON.stringify(logInsertList));
			$('#beforProjectForm input[name=partnerInsertListJson]').val(JSON.stringify(partnerInsertList));
		},
		/**添加合作商和合同信息*/
		putImplFormJson: function(){
			var contractBillingUpdateList = [];
			var contractBillingInsertList = [];
			var contractPaymentUpdateList = [];
			var contractPaymentInsertList = [];
			var partnerUpdateList = [];
			var partnerDetailUpdateList = [];
			$('#contractBillingDetailTable tbody tr[x-update]').each(function(i, v){
				var oi = {};
				$(this).find('input').each(function(i, v){
					if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
				});
				$(this).find('select').each(function(i,v){
					oi[this.name] = $(this).val();
				});
				contractBillingUpdateList[i] = oi;
		    });
			$('#contractBillingDetailTable tbody tr[x-insert]').each(function(i, v){
		        var oi = {};
		        $(this).find('input').each(function(i, v){
		        	if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
		        });
		        $(this).find('select').each(function(i,v){
					oi[this.name] = $(this).val();
				});
		        contractBillingInsertList[i] = oi;
		        console.log(oi);
		    });
			$('#contractPaymentDetailTable tbody tr[x-update]').each(function(i, v){
				var oi = {};
				$(this).find('input').each(function(i, v){
					if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
				});
				$(this).find('select').each(function(i,v){
					oi[this.name] = $(this).val();
				});
				contractPaymentUpdateList[i] = oi;
		    });
			$('#contractPaymentDetailTable tbody tr[x-insert]').each(function(i, v){
		        var oi = {};
		        $(this).find('input').each(function(i, v){
		        	if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
		        });
		        $(this).find('select').each(function(i,v){
					oi[this.name] = $(this).val();
				});
		        contractPaymentInsertList[i] = oi;
		        console.log(oi);
		    });
			$('#partnerCompanyTable tbody tr').each(function(i, v){
				var oi = {};
				$(this).find('select').each(function(i, v){
					oi[this.name] = $(this).val();
				});
				partnerUpdateList[i] = oi;
		    });
			$('#partnerTable tbody tr').each(function(i, v){
				var oi = {};
		        $(this).find('input').each(function(i, v){
		        	if(this.type == 'checkbox'){
						oi[this.name] = this.checked ? 'Y':'N';
					}else{
						oi[this.name] = this.value;
					}
		        });
		        $(this).find('select').each(function(i,v){
					oi[this.name] = $(this).val();
				});
		        partnerDetailUpdateList[i] = oi;
			});
			$('#implProjectForm input[name=contractBillingUpdateListJson]').val(JSON.stringify(contractBillingUpdateList));
			$('#implProjectForm input[name=contractBillingInsertListJson]').val(JSON.stringify(contractBillingInsertList));
			$('#implProjectForm input[name=contractPaymentUpdateListJson]').val(JSON.stringify(contractPaymentUpdateList));
			$('#implProjectForm input[name=contractPaymentInsertListJson]').val(JSON.stringify(contractPaymentInsertList));
			$('#implProjectForm input[name=partnerUpdateListJson]').val(JSON.stringify(partnerUpdateList));
			$('#implProjectForm input[name=partnerDetailUpdateListJson]').val(JSON.stringify(partnerDetailUpdateList));
			
		},
		changeCheckbox : function(obj){
			var value = $(obj).val();
			if(""==value || "N"==value){
				$(obj).val("Y");
			}else{
				$(obj).val("N");
			}
		},
		/**售前和执行项目列表分页查询*/
		projectPage : function(currentPage){
			var projectName = $("#projectName").val();
			var customerCompanyId = $("#customerCompanyId").val();
			var staffId = $("#staffId1").val();
			var partnerCompanyName = $("#partnerCompanyName").val();
			var projectStatus = $("#projectStatus").val();
			$.ajax({
				url  : project.ctx + "/project/getProjectListByPage",
				type : "post",
				async:false,
				dataType:"json", 
				data: {	"currentPage":currentPage,
						"projectName":projectName,
						"customerCompanyId":customerCompanyId,
						"staffId":staffId,
						"partnerCompanyName":partnerCompanyName,
						"projectStatus":projectStatus
				},
				success:function(data){
					var str = "";
					for(var i=0;i<data.length;i++){
						var config = data[i];
						if(data[i].projectStatus=='1'||data[i].projectStatus=='2'){
							str = str 	+ "<tr><td>"+data[i].projectId+"</td>";
							if('1'==data[i].projectStatus){
								str = str + "<td><a href = \" "+project.ctx +"/project/projectBeforeFormPage?projectId="+data[i].projectId+"\" >"+data[i].projectName+"</a></td>";
							}else if('2'==data[i].projectStatus){
								str = str + "<td><a href = \" "+project.ctx +"/project/projectImplementFormPage?projectId="+data[i].projectId+"\" >"+data[i].projectName+"</a></td>"
							}
							str = str 	+ "<td>"+data[i].customerCompanyName+"</td>"
										+ "<td>"+data[i].staffName+"</td>"
										;
							if(null==data[i].partnerCompanyName){
								str = str 	+ "<td></td>";
							}else{
								str = str + "<td>"+data[i].partnerCompanyName+"</td>";
							}
							str = str 	+ "<td>"+data[i].projectStatusValue+"</td></tr>";
						}
						
					}
					$('#tableList').html(str);
					var container = $('#page');
					project.setPage(container, $("#count").val(), currentPage,"project.projectPage");
					
		       	},
		       	error: function(){ 
		       		$('#error').val("出现异常！");
					$('#errorMsg').html("出现异常,或没有找到相关数据");
					var $modal1 = $('#my-alert');
					$modal1.modal();
		        }
			}); 
		},
		/**全部项目列表分页查询*/
		queryProjectPage : function(currentPage){
			var projectName = $("#projectName").val();
			var customerCompanyId = $("#customerCompanyId").val();
			var staffId = $("#staffId1").val();
			var partnerCompanyName = $("#partnerCompanyName").val();
			var projectStatus = $("#projectStatus").val();
			$.ajax({
				url  : project.ctx + "/project/getQueryProjectListByPage",
				type : "post",
				async:false,
				dataType:"json", 
				data: {	"currentPage":currentPage,
					"projectName":projectName,
					"customerCompanyId":customerCompanyId,
					"staffId":staffId,
					"partnerCompanyName":partnerCompanyName,
					"projectStatus":projectStatus
				},
				success:function(data){
					var str = "";
					if(data.list != null && data.list.length != 0) {
						for(var i=0;i<data.list.length;i++){
							str = str 	+ "<tr><td>"+data.list[i].projectId+"</td>"
										+ "<td><a href = \" "+project.ctx +"/project/projectQueryFormPage?projectId="+data.list[i].projectId+" \" >"+data.list[i].projectName+"</a></td>"
										+ "<td>"+data.list[i].customerCompanyName+"</td>"
										+ "<td>"+data.list[i].staffName+"</td>"
										;
							if(null==data.list[i].partnerCompanyName){
								str = str 	+ "<td></td>";
							}else{
								str = str + "<td>"+data.list[i].partnerCompanyName+"</td>";
							}
							str = str 	+ "<td>"+data.list[i].projectStatusValue+"</td></tr>";
							
						}
					}
					$('#tableList').html(str);
					
					var container = $('#page');
					project.setPage(container, data.count, currentPage,"project.queryProjectPage");
		       	},
		       	error: function(){ 
		       		$('#error').val("出现异常！");
					$('#errorMsg').html("出现异常,或没有找到相关数据");
					var $modal1 = $('#my-alert');
					$modal1.modal();
		        }
			}); 
		},
		/**全部项目列表分页查询【管理员】*/
		queryProjectPageAdmin : function(currentPage){
			var projectName = $("#projectName").val();
			var customerCompanyId = $("#customerCompanyId").val();
			var staffId = $("#staffId1").val();
			var partnerCompanyName = $("#partnerCompanyName").val();
			var projectStatus = $("#projectStatus").val();
			$.ajax({
				url  : project.ctx + "/project/getQueryProjectListByPage",
				type : "post",
				async:false,
				dataType:"json", 
				data: {	"currentPage":currentPage,
					"projectName":projectName,
					"customerCompanyId":customerCompanyId,
					"staffId":staffId,
					"partnerCompanyName":partnerCompanyName,
					"projectStatus":projectStatus
				},
				success:function(data){
					var str = "";
					if(data.list != null && data.list.length != 0) {
						for(var i=0;i<data.list.length;i++){
							str = str 	+ "<tr><td>"+data.list[i].projectId+"</td>"
										+ "<td><a href = \" "+project.ctx +"/project/projectImplementFormPageAdmin?projectId="+data.list[i].projectId+" \" >"+data.list[i].projectName+"</a></td>"
										+ "<td>"+data.list[i].customerCompanyName+"</td>"
										+ "<td>"+data.list[i].staffName+"</td>"
										;
							if(null==data.list[i].partnerCompanyName){
								str = str + "<td></td>";
							}else{
								str = str + "<td>"+data.list[i].partnerCompanyName+"</td>";
							}
							str = str + "<td>"+data.list[i].projectStatusValue+"</td></tr>";
							
						}
					}
					$('#tableList').html(str);
					
					var container = $('#page');
					project.setPage(container, data.count, currentPage, "project.queryProjectPageAdmin");
		       	},
		       	error: function(){ 
		       		$('#error').val("出现异常！");
					$('#errorMsg').html("出现异常,或没有找到相关数据");
					var $modal1 = $('#my-alert');
					$modal1.modal();
		        }
			}); 
		},
		/**分页工具*/
		//container 容器,count 总页数, pageindex 当前页数,action 调用的查询方法名称
		setPage : function(container, count, pageindex,action) {
			var container = container;
			var count = count;
			var pageindex = pageindex;
			var a = [];
			
			  //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
			  if (pageindex == 1) {
				a[a.length] = "<li class=\"am-disabled\"><a href=\"javascript:void(0);\" class=\"prev unclick\" onclick=\""+action+"('1')\"> « </a></li>";
			  } else {
				a[a.length] = "<li><a href=\"javascript:void(0);\" class=\"prev\" onclick=\" "+action+"('"+(pageindex-1)+"')\"> « </a></li>";
			  }
			  function setPageList() {
				if (pageindex == i) {
				  a[a.length] = "<li class=\"am-active\"><a href=\"javascript:void(0);\" onclick=\""+action+"('"+i+"')\">" + i + "</a></li>";
				} else {
				  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+i+"')\">" + i + "</a></li>";
				}
			  }
			  //总页数小于5
			  if (count <= 4) {
				for (var i = 1; i <= count; i++) {
				  setPageList();
				}
			  }
			  //总页数大于4页
			  else {
				if (pageindex <= 3) {
				  for (var i = 1; i <= 3; i++) {
					setPageList();
				  }
				  a[a.length] = "<li>......&nbsp;&nbsp;</li><li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+count+"')\">" + count + "</a></li>";
				} else if (pageindex >= count - 2) {
				  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('1')\">1</a></li><li>......&nbsp;&nbsp;</li>";
				  for (var i = count - 2; i <= count; i++) {
					setPageList();
				  }
				} else { //当前页在中间部分
				  a[a.length] = "<li><a href=\"javascript:void(0);\" onclick=\""+action+"('1')\">1</a></li><li>......&nbsp;&nbsp;</li>";
				  for (var i = pageindex - 1; i <= pageindex + 1; i++) {
					setPageList();
				  }
				  a[a.length] = "<li>......&nbsp;&nbsp;</li><li><a href=\"javascript:void(0);\" onclick=\""+action+"('"+count+"')\">" + count + "</a></li>";
				}
			  }
			  if (pageindex == count) {
				a[a.length] = "<li class=\"am-disabled\"><a href=\"javascript:void(0);\" class=\"next unclick\" onclick=\""+action+"('"+count+"')\"> » </a></li>";
			  } else {
				  var addindex = parseInt(pageindex) + 1;
				a[a.length] = "<li><a href=\"javascript:void(0);\" class=\"next\" onclick=\""+action+"('"+addindex+"')\"> » </a></li>";
			  }
			  
			  if(count == 0) {
				  container.html([]);
			  }else {
				  container.html(a);
			  }
		},
		isNum : function(obj){
			reg=/^[0-9]+$/;  
			if(!reg.test(obj)){   
	            $('#errorMsg').html("报价只能输入数字！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
				return false;
	        }
			return true;
		},
		initRate : function(){
			$("input[name=billingMoney]").each(function(i, v){
				$(this).blur(function(){
					  var rate = (Number($(this).val())/$("#offer").val());
					  var rrate = project.roundRate(rate,4);
					  var lrrate = project.roundRate(rrate*100,2);
					  
					  $(this).parent().parent().next().find("input").val(lrrate);
				});
			});
		},
		roundRate : function(digit, length){
			length = length ? parseInt(length) : 0;
		    if (length <= 0) return Math.round(digit);
		    digit = Math.round(digit * Math.pow(10, length)) / Math.pow(10, length);
		    return digit;
		}
		/*,
		resetForm  : function(){
			$("#projectName").val("");
			$("#customerCompanyId").val("");
			$("#staffId1").val("");
			$("#partnerCompanyName").val("");
			$("#projectStatus").val("");
		}*/
}